﻿package igz.fleaxy.ui 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	import com.greensock.TweenMax;
	import igz.fleaxy.Fleaxy;
	import igz.fleaxy.util.ObjectUtil;
	
	/**
	 * @author Intelygenz | www.intelygenz.com
	 */
	public class ShieldUi extends Sprite {

		private var _Settings : Object;
		
		private var _Bitmap : Sprite;
		private var _Target : DisplayObjectContainer;
		private var _TargetFilters : Array;
	
		public function ShieldUi() {
			
		}

		/**
		 * Crea un escudo con una animación de emborronamiento
		 * @param	p_target Objetivo al que se le aplicará el escudo, si se omite se realizará al stage principal
		 * @param	p_settings {  "duration" : 1
								, "blurX" : 5
								, "blurY" : 5
								, "filterTypes" : null // Array of filers (see igz.fleaxy.util.FilterType for examples)
								}
		   @example
				// Nota: Si intentamos aplicar filtros al stage, nos dará error, debe ser una clase que herede las propiedades de DisplayObject	
				_ShieldUi.$Show(Gui.$Current.$MainWindowUi, {"filterTypes":[FilterType.$GRAYSCALE, new DropShadowFiler()]});
		 */
		public function $Show ( p_target:DisplayObjectContainer = null, p_settings:Object = null ) : void {

			_Settings = { "duration" : 1
						, "blurX" : 5
						, "blurY" : 5
						, "filterTypes" : null // Array of filters (see igz.fleaxy.util.FilterType for examples)
			};
			ObjectUtil.$Merge( p_settings, _Settings );

			var targetWidth : Number; 
			var targetHeight : Number;
			if ( p_target != null ) {
				_Target = p_target;
				targetWidth = _Target.width;
				targetHeight = _Target.height;
			}
			else {
				_Target = Fleaxy.$Current.$Stage;
				targetWidth = Fleaxy.$Current.$Stage.stageWidth;
				targetHeight = Fleaxy.$Current.$Stage.stageHeight;
			}
			_Target.addEventListener( Event.RESIZE, _OnTargetResized );
			if ( _Settings["filterTypes"] is Array ) {
				_TargetFilters = _Target.filters;
				_Target.filters = _Target.filters.concat( _Settings["filterTypes"] as Array );
			}

			var bmpData : BitmapData;
			bmpData = new BitmapData( targetWidth, targetHeight, true, 0 );
			bmpData.draw( _Target );
			_Bitmap = new Sprite();
			_Bitmap.addChild( new Bitmap(bmpData) );
			_Bitmap.addEventListener(MouseEvent.MOUSE_DOWN, _StopMouseDown);
			_Target.addChild( _Bitmap );

			TweenMax.killTweensOf( _Bitmap );
			TweenMax.to( _Bitmap, _Settings["duration"]
									, {
									   "blurFilter" : { "blurX" : _Settings["blurX"], "blurY" : _Settings["blurY"] }
									 , "alpha" : 1
									}
						);
		}

		private function _StopMouseDown ( p_mouseEvent:MouseEvent ) : void {
			p_mouseEvent.stopPropagation();
		}
		
		private function _OnTargetResized ( p_event:Event ) : void {
			if ( _Target == Fleaxy.$Current.$Stage ) {
				_Bitmap.width = Fleaxy.$Current.$Stage.stageWidth;
				_Bitmap.height = Fleaxy.$Current.$Stage.stageHeight;
			}
			else {
				_Bitmap.width = _Target.width;
				_Bitmap.height = _Target.height;
			}
		}

		/**
		 * Oculta el escudo
		 * @param	p_settings { "duration" : 1 }
		 */
		public function $Hide( p_settings:Object = null ) : void {
			_Settings = { "duration" : 1 }
			ObjectUtil.$Merge( p_settings, _Settings );

			_Target.removeEventListener( Event.RESIZE, _OnTargetResized );
			
			TweenMax.killTweensOf(_Bitmap);
			if ( _Bitmap != null ) {
				_Bitmap.removeEventListener(MouseEvent.MOUSE_DOWN, _StopMouseDown);
				TweenMax.to( _Bitmap, _Settings["duration"], { blurFilter : { blurX : 1, blurY : 1 } 
										 , alpha : 0
										 , onComplete : _Hide_OnComplete
										 } 
							);
			}
			
			if ( _TargetFilters != null ) {
				_Target.filters = _TargetFilters;
			}
		}

		private function _Hide_OnComplete() : void {
			if ( (_Bitmap != null) && (_Bitmap.parent != null) ) {
				_Bitmap.parent.removeChild(_Bitmap);
				_Bitmap = null;
			}
		}

	}
	
}